import dt from '@dt/dt'
import buildingApi from '@/config/server/api/building.js'
import customerApi from '@/config/server/api/customer.js'
import userApi from '@/config/server/api/user.js'
import baseApi from '@/config/server/api/base.js'
import biApi from '@/config/server/api/bi.js'
import channelMgr from '@/biz/channel/channel'

let consultantsList = [],
  consultantsList1 = [],
  intentionProductList = [],
  statusList = [],
  intentionHouseTypeList = [],
  intentionAreaList = [],
  intentionTotalPriceList = [],
  intentionAveragePriceList = [],
  channelSourceList = [],
  intentionLevelList = [],
  intentionPurposeList = [],
  intentionRegionList = []

// function columns() {
//   return customerApi.clue.clueHeadList.fetch({
//     buildingId: dt.buildInfo().housesId
//   }).then(res => {
//     let data = []
//     res.showHead = res.showHead.map(item => {
//       let obj = {
//         field: item.fieldKey,
//         title: item.fieldValue,
//         minWidth: 130
//       }
//       if (item.fieldKey == 'customerName') { // 客户姓名
//         data[0] = obj
//       } else if (item.fieldKey == 'customerPhone') { // 手机号码
//         data[1] = obj
//       } else if (item.fieldKey == 'attributionConsultant') { // 归属顾问
//         data[2] = obj
//       } else if (item.fieldKey == 'intentionProduct') { // 意向业态
//         intentionProductList = item.options.map(i => ({
//           key: i.optionId,
//           value: i.optionValue
//         }))
//       } else if (item.fieldKey == 'status') { // 客户进度
//         statusList = item.options.map(i => ({
//           key: i.optionId,
//           value: i.optionValue
//         }))
//       } else if (item.fieldKey == 'intentionHouseType') { // 意向户型
//         intentionHouseTypeList = item.options.map(i => ({
//           key: i.optionId,
//           value: i.optionValue
//         }))
//       } else if (item.fieldKey == 'intentionLevel') { // 意向等级
//         intentionLevelList = item.options.map(i => ({
//           key: i.optionId,
//           value: i.optionValue
//         }))
//       } else if (item.fieldKey == 'intentionPurpose') { // 置业目的
//         intentionPurposeList = item.options.map(i => ({
//           key: i.optionId,
//           value: i.optionValue
//         }))
//       }
//       return obj
//     })

//     return [
//       ...data,
//       ...res.showHead.filter(i => i.field != 'customerName' && i.field !=
//         'customerPhone' && i.field != 'attributionConsultant')
//     ]
//   })
// }
function clueHeadUpdate(info) {
  return customerApi.clue.clueHeadUpdate.fetch(info).then((res) => {
    return res
  })
}

function columns() {
  return customerApi.clue.clueHeadList
    .fetch({
      buildingId: dt.buildInfo().housesId
    })
    .then((res) => {
      let data = [],
        showHead = res.showHead,
        hiedHead = res.hiedHead

      res.showHead = res.showHead.map((item) => {
        item.disabled = false
        let obj = {
          field: item.fieldKey,
          title: item.fieldValue,
          minWidth: 150,
          maxWidth: 300,
          dbKey: item.dbKey,
          fieldValue: item.fieldValue
        }
        if (item.fieldKey == 'customerName') {
          // 客户姓名
          item.disabled = true
          data[0] = obj
        } else if (item.fieldKey == 'customerPhone') {
          // 手机号码
          item.disabled = true
          data[1] = obj
        } else if (item.fieldKey == 'attributionConsultant') {
          // 归属顾问
          item.disabled = true
          data[2] = obj
        } else if (item.fieldKey == 'intentionProduct') {
          // 意向业态
          intentionProductList = item.options.map((i) => ({
            key: i.optionId,
            value: i.optionValue
          }))
        } else if (item.fieldKey == 'status') {
          // 客户进度
          statusList = item.options.map((i) => ({
            key: i.optionId,
            value: i.optionValue
          }))
        } else if (item.fieldKey == 'intentionHouseType') {
          // 意向户型
          intentionHouseTypeList = item.options.map((i) => ({
            key: i.optionId,
            value: i.optionValue
          }))
        } else if (item.fieldKey == 'intentionLevel') {
          // 意向等级
          intentionLevelList = item.options.map((i) => ({
            key: i.optionId,
            value: i.optionValue
          }))
        } else if (item.fieldKey == 'intentionPurpose') {
          // 置业目的
          intentionPurposeList = item.options.map((i) => ({
            key: i.optionId,
            value: i.optionValue
          }))
        }
        return obj
      })
      let tmp = {
        resArr: [
          ...data,
          ...res.showHead.filter(
            (i) =>
              i.field != 'customerName' &&
              i.field != 'customerPhone' &&
              i.field != 'attributionConsultant'
          )
        ],
        hiedHead: hiedHead,
        showHead: showHead
      }
      return tmp
    })
}

function list(info) {
  return customerApi.clue.list
    .fetch({
      roleId: dt.userInfo().role.roleId,
      buildingId: dt.buildInfo().housesId,
      ...info
    })
    .then((res) => {
      res.list = res.records
      return res
    })
}

function count(info) {
  return customerApi.clue.count
    .fetch({
      roleId: dt.userInfo().role.roleId,
      buildingId: dt.buildInfo().housesId,
      ...info
    })
    .then((res) => {
      return res
    })
}

function exportData(info) {
  info.queryWebCustomerListDTO.buildingId = dt.buildInfo().housesId
  info.queryWebCustomerListDTO.roleId = dt.userInfo().role.roleId
  info.queryWebCustomerListDTO.page = 1
  info.queryWebCustomerListDTO.rows = 9999
  delete info.queryWebCustomerListDTO.current
  return customerApi.clue.export.fetch(info)
}

function distribution(info) {
  info.buildingId = dt.buildInfo().housesId
  return customerApi.clue.distribution.fetch(info)
}

function customerInfo(info) {
  return customerApi.clue.info.fetch(info)
}

function selectTeamUser(name, isBuild = true) {
  let params = {
    buildingId: isBuild ? dt.buildInfo().housesId : null,
    userNameOrWorkNo: name || ''
  }
  return userApi.selectTeamUser.fetch(params).then((res) => {
    let data = res.list.map((item) => ({
      ...item,
      key: item.userId,
      value: `${item.userName}(${item.workNo})-${
        item.status == '1' ? '在案' : '离案'
      }`
    }))
    consultantsList1 = data
    return data
  })
}

function getConsultant(name) {
  return userApi.counselorList
    .fetch({
      buildingId: dt.buildInfo().housesId,
      userName: name || ''
    })
    .then((res) => {
      let data = res.map((item) => ({
        ...item,
        key: item.userId,
        value: item.userName + '(' + item.workNo + ')'
      }))
      consultantsList = data
      return data
    })
}

function adviserList(info) {
  return userApi.customer.adviserList
    .fetch({
      buildingId: dt.buildInfo().housesId,
      onlineType: info.onlineType ? info.onlineType : 1,
      haveRole: 1
    })
    .then((res) => {
      return res
    })
}

function consultantList(info) {
  return userApi.customer.adviserList.fetch(info).then((res) => {
    return res
  })
}

function getFilterOptions() {
  return buildingApi.generalConfiguration.selectAggregationCriteria
    .fetch({
      cityCode: dt.buildInfo().cityId,
      housesId: dt.buildInfo().housesId
    })
    .then((res) => {
      intentionAreaList = res.areaList.map((i) => ({
        key: i.searchParamId,
        value: i.paramValues
      }))
      intentionTotalPriceList = res.totalList.map((i) => ({
        key: i.searchParamId,
        value: i.paramValues
      }))
      intentionAveragePriceList = res.priceList.map((i) => ({
        key: i.searchParamId,
        value: i.paramValues
      }))
      return res
    })
}

function getChannel(isBuild = true) {
  if (isBuild) {
    let params = {
      page: 1,
      rows: 100,
      cityCode: dt.buildInfo().cityId,
      buildingId: dt.buildInfo().housesId,
      userId: dt.userInfo().userId
    }
    return baseApi.caseChannel.caseChannelList.fetch(params).then((res) => {
      let data = res.list.map((i) => ({
        key: i.channelId,
        value: i.channelName
      }))
      channelSourceList = data
      return data
    })
  } else {
    return channelMgr.getClueChannelList({}).then((res) => {
      let data = res.map((i) => ({
        key: i.id,
        value: i.channelName
      }))
      data.unshift({
        key: -1,
        value: '非线索渠道'
      })
      channelSourceList = data
      return data
    })
  }
}

function getArea() {
  return baseApi.city.cityBaseList
    .fetch({
      parentCode: dt.buildInfo().cityId,
      level: 4
    })
    .then((res) => {
      let data = res.list.map((i) => ({
        key: i.cityId,
        value: i.cityName
      }))
      intentionRegionList = data
      return data
    })
}

function follow(info) {
  info.buildingId = info.buildingId ? info.buildingId : dt.buildInfo().housesId
  info.operatorId = dt.userInfo().userId
  return customerApi.customer.followUp.fetch(info)
}

function updateFollow(info) {
  console.log(info, '22bbbbb')
  info.buildingId = info.buildingId ? info.buildingId : dt.buildInfo().housesId
  info.operatorId = dt.userInfo().userId
  return customerApi.customer.updateFollowUp.fetch(info)
}

function dealRange(obj, unit) {
  let str = ''
  if (obj.start == '' || obj.start == '0') {
    str = obj.end + unit + '以下'
  } else if (obj.end == '' || obj.end > 99999) {
    str = obj.start + unit + '以上'
  } else {
    str = obj.start + '~' + obj.end + unit
  }
  return str
}

function consultants() {
  return consultantsList
}

function consultants1() {
  return consultantsList1
}

function intentionProduct() {
  return intentionProductList
}

function status() {
  return statusList
}

function intentionHouseType() {
  return intentionHouseTypeList
}

function intentionArea() {
  return intentionAreaList
}

function intentionTotalPrice() {
  return intentionTotalPriceList
}

function intentionAveragePrice() {
  return intentionAveragePriceList
}

function channelSource() {
  return channelSourceList
}

function intentionLevel() {
  return intentionLevelList
}

function intentionPurpose() {
  return intentionPurposeList
}

function intentionRegion() {
  return intentionRegionList
}

// 线索记录
function counselorRecord(info) {
  info.buildingId = dt.buildInfo().housesId
  return customerApi.clue.counselorRecord.fetch(info)
}
// 线索记录总数
function customerVisitRecord() {
  return customerApi.clue.customerVisitRecord.fetch({
    buildingId: dt.buildInfo().housesId
  })
}

function moreCounselorRecord(info) {
  info.buildingId = info.buildingId ? info.buildingId : dt.buildInfo().housesId
  return customerApi.clue.moreCounselorRecord.fetch(info)
}

function culeCount(info) {
  return biApi.clue.count.fetch(info)
}

function culeRecordList(info) {
  return biApi.clue.list.fetch(info)
}

function culeCount1(info) {
  return biApi.clue.count1.fetch(info)
}

function culeRecordList1(info) {
  return biApi.clue.list1.fetch(info)
}

function clueRecordExport(info) {
  return biApi.clue.caseRecordExport.fetch(info)
}

function recordListExport(info) {
  return biApi.clue.recordListExport.fetch(info)
}

function visitRecordExport(info) {
  info.buildingId = dt.buildInfo().housesId
  return customerApi.clue.visitRecordExport.fetch(info)
}

function clueCustomerList(info) {
  info.buildingId = dt.buildInfo().housesId
  return customerApi.clue.clueCustomerList.fetch(info)
}

function clueCustomerCount(info) {
  info.buildingId = dt.buildInfo().housesId
  return customerApi.clue.clueCustomerCount.fetch(info)
}

function customerClueDistribution(info) {
  info.buildingId = dt.buildInfo().housesId
  return customerApi.clue.customerClueDistribution.fetch(info)
}

// 分配线索客户

function distributeClue(info) {
  info.buildingId = dt.buildInfo().housesId
  return customerApi.clue.distributeClue.fetch(info)
}

function customerClueDistributionExport(info) {
  info.buildingId = dt.buildInfo().housesId
  return customerApi.clue.customerClueDistributionExport.fetch(info)
}

function clueListExport(info) {
  return customerApi.clue.clueListExport.fetch(info)
}

function isMoreCounselor(info) {
  console.log(info, '11111')
  info.buildingId = info.buildingId ? info.buildingId : dt.buildInfo().housesId
  return customerApi.clue.isMoreCounselor.fetch(info)
}

//查询无效原因文案列表
function getInvalidCauseList(info) {
  return customerApi.clue.getInvalidCauseList.fetch(info)
}

//线索客户分配列表
function clueDistributionList(info) {
  return customerApi.clue.clueDistributionList.fetch(info)
}

//线索客户分配列表统计
function clueDistributionListCount(info) {
  return customerApi.clue.clueDistributionListCount.fetch(info)
}

//设为无效客户
function setInvalidCustomer(info) {
  return customerApi.clue.setInvalidCustomer.fetch(info)
}

//到访分配统计
function customerListAssignmentCount(info) {
  return customerApi.customer.customerListAssignmentCount.fetch(info)
}
//线索分配统计
function clueAssignmentostClueCount(info) {
  return customerApi.clue.clueAssignmentostClueCount.fetch(info)
}

//公客池分配统计
function customerPublicPoolAssignmentCount(info) {
  return customerApi.publicCustomer.customerPublicPoolAssignmentCount.fetch(
    info
  )
}

//线索分配
function clueDistributeClue(info) {
  return customerApi.clue.clueDistributeClue.fetch(info)
}
// 到访分配
function visitDistribute(info) {
  return customerApi.customer.visitDistribute.fetch(info)
}

export default {
  columns,
  list,
  count,
  exportData,
  distribution,
  customerInfo,
  getConsultant,
  // 顾问
  selectTeamUser,
  adviserList,
  getFilterOptions,
  getChannel,
  getArea,
  follow,
  updateFollow,

  consultants,
  consultants1,
  intentionProduct,
  status,
  intentionHouseType,
  intentionArea,
  intentionTotalPrice,
  intentionAveragePrice,
  channelSource,
  intentionLevel,
  intentionPurpose,
  intentionRegion,
  // 线索记录
  counselorRecord,
  customerVisitRecord,
  moreCounselorRecord,
  culeCount,
  culeRecordList,
  culeCount1,
  culeRecordList1,
  clueRecordExport,
  recordListExport,
  visitRecordExport,
  clueCustomerList,
  clueCustomerCount,
  customerClueDistribution,
  distributeClue,
  customerClueDistributionExport,
  clueHeadUpdate,
  clueListExport,
  isMoreCounselor,
  getInvalidCauseList,
  clueDistributionList,
  clueDistributionListCount,
  setInvalidCustomer,

  customerListAssignmentCount, //到访分配统计
  clueAssignmentostClueCount, //线索分配统计
  customerPublicPoolAssignmentCount, //公客池分配统计
  consultantList,
  clueDistributeClue,
  visitDistribute
}
